package cn.yuanqiao.web.controller.archive.liyong;

import cn.yuanqiao.archive.liyong.domain.ListQuery;
import cn.yuanqiao.archive.liyong.domain.YqVisitkeywords;
import cn.yuanqiao.archive.liyong.service.ListQueryService;
import cn.yuanqiao.common.core.page.TableDataInfo;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.PageDomain;
import cn.yuanqiao.common.web.page.TableSupport;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 目录检索
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.archive.liyong.controller
 * @ClassName: ListQueryController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/6/27 11:21
 * @Version: 1.0
 */
@RestController
@RequestMapping("/archive/list/query")
public class ListQueryController extends BaseController {
    @Autowired
    ListQueryService listQueryService;

    /**
     * 目录检索接口
     *
     * @param
     * @return cn.yuanqiao.common.core.web.domain.AjaxResult
     * @author hbWang
     * @date 2023/6/28 9:09
     */
    @PreAuthorize("@ss.hasPermi('archive:list:query')")
    @PostMapping("/query")
    public TableDataInfo query(@RequestBody() ListQuery listQuery) throws Exception {
        //CompletableFuture
        PageDomain pageDomain = TableSupport.buildPageRequest();
        listQuery.setPageDomain(pageDomain);
        return listQueryService.query(listQuery);
    }

    /**
     * 目录检索接口
     *
     * @param
     * @return cn.yuanqiao.common.core.web.domain.AjaxResult
     * @author hbWang
     * @date 2023/6/28 9:09
     */
    @PreAuthorize("@ss.hasPermi('archive:list:query')")
    @PostMapping("/queryCount")
    public AjaxResult queryCount(@RequestBody() ListQuery listQuery) throws Exception {
        return AjaxResult.success(listQueryService.queryCount(listQuery));
    }

    /**
     * 查询全宗信息
     *
     * @param menuId
     * @return
     */
    @PostMapping("/fonds/list")
    public AjaxResult queryCategory(@RequestParam("menuId") String menuId) {
        return AjaxResult.success(listQueryService.queryFonds(menuId));
    }

    /**
     * 查询门类
     *
     * @param type
     * @return
     */
    @PostMapping("/category/list")
    public AjaxResult queryCategory(@RequestParam(required = false, value = "type") String type, @RequestParam("menuId") String menuId, @RequestParam(required = false, value = "qzh") String qzh) {
        if ("-1".equals(qzh)) {
            AjaxResult.success();
        }
        return AjaxResult.success(listQueryService.queryCategory(type, menuId, qzh));
    }

    /**
     * 查询展示列
     *
     * @return
     */
    @PostMapping("/field/list")
    public AjaxResult queryFiels(@RequestBody() JSONObject json) {
        return AjaxResult.success(listQueryService.queryFields(json));
    }

    /**
     * 查询条件
     *
     * @param json
     * @return
     */
    @PostMapping("/apply/list")
    public AjaxResult queryApply(@RequestBody() JSONObject json) {
        return AjaxResult.success(listQueryService.queryApply(json));
    }

    /**
     * 查询前端展示分类
     *
     * @return
     */
    @PostMapping("/fication/list")
    public AjaxResult queryFication(@RequestParam(value = "menuId", required = false) String menuId) {
        return AjaxResult.success(listQueryService.queryFication(menuId));
    }


    /**
     * 获取关键词检索历史表详细信息
     */
    @PostMapping(value = "/key/words")
    public AjaxResult keyWords(@RequestBody() YqVisitkeywords yqVisitkeywords) {
        return AjaxResult.success(listQueryService.selectYqVisitkeywordsList(yqVisitkeywords));
    }

    /**
     * 新增关键词
     */
    @Log(title = "关键词检索历史表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add/words")
    public AjaxResult addKeywords(@RequestBody() JSONObject json) {
        String keyWords = json.getString("keyWords");
        String menuId = json.getString("menuId");
        return toAjax(listQueryService.insertYqVisitkeywords(keyWords, menuId));
    }


    /**
     * 批量下载电子原文
     */
    @Log(title = "批量下载电子原文", businessType = BusinessType.DOWNLOAD)
    @PostMapping(value = "/download/electronics")
    public void downloadElectronics(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String dalist = request.getParameter("dalist");
        JSONObject json = JSONObject.parseObject(dalist);
        listQueryService.downloadElectronics(json, response);
    }

}
